﻿// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 
// XMLocalizator 0.9
// This code is under The MIT Licence (http://xmlocalizator.codeplex.com/license).
// Copyright (c) 2011 Bartosz Lenar (http://bartoszlenar.com/).
//
// For the full list of people involved in this project, visit:
// http://xmlocalizator.codeplex.com/wikipage?title=About
//
// The latest release, documentation, examples and demo applications always available on:
// http://xmlocalizator.codeplex.com/
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 

using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace XMLocalizator.XmlElements
{
    /// <summary>
    /// The core XML element, representing a single localization. 
    /// Object of this class contains everything that is needed to localize an application, and serialized as a XML file is in fact the localization file.
    /// </summary>
    [XmlRoot(Namespace = "XMLocalizator", ElementName = "Localization")]
    public sealed class Localization
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Localization"/> class.
        /// </summary>
        public Localization()
        {
            Phrases = new List<Phrase>();
        }

        /// <summary>
        /// Gets or sets information about this localization's language.
        /// </summary>
        /// <value>
        /// The language of this localization.
        /// </value>
        public String Language { get; set; }

        /// <summary>
        /// Gets or sets information about the language tag.
        /// </summary>
        /// <value>
        /// The language tag.
        /// </value>
        public String LanguageTag { get; set; }

        /// <summary>
        /// Gets or sets the author(s) of this localization.
        /// </summary>
        /// <value>
        /// The author(s) of this localization.
        /// </value>
        public String Author { get; set; }

        /// <summary>
        /// Gets or sets the name of the application which this localization is created for.
        /// </summary>
        /// <value>
        /// The name of the application which this localization is created for.
        /// </value>
        public String Application { get; set; }

        /// <summary>
        /// Gets or sets the version of the application which this localization is created for.
        /// </summary>
        /// <value>
        /// The version of the application which this localization is created for.
        /// </value>
        public String ApplicationVersion { get; set; }

        /// <summary>
        /// Gets or sets the version of this localization.
        /// </summary>
        /// <value>
        /// The version of this localization.
        /// </value>
        public String LocalizationVersion { get; set; }

        /// <summary>
        /// Gets or sets the custom data.
        /// </summary>
        /// <value>
        /// The custom data.
        /// </value>
        public String CustomData { get; set; }

        /// <summary>
        /// Gets or sets the collection of phrases.
        /// </summary>
        /// <value>
        /// The collection phrases.
        /// </value>
        public List<Phrase> Phrases { get; set; }


        /// <summary>
        /// Saves this localization as the XML file.
        /// </summary>
        /// <param name="filePath">The path where XML file with localization should be created.</param>
        public void SaveAsXml(String filePath)
        {
            using (Stream xmlFile = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (XmlTextWriter xmlContent = new XmlTextWriter(xmlFile, Encoding.UTF8))
                {
                    xmlContent.Formatting = Formatting.Indented;

                    _serializer.Serialize(xmlContent, this, _xmlNamespace);
                }
            }
        }

        // ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 
        // ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 
        // ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 
        // ---------- ---------- !! !! !! !!  PRIVATE PART BELOW !! !! !! !! ---------- ---------- 
        // ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 
        // ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 
        // ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 

        static Localization()
        {
            _xmlNamespace.Add(String.Empty, "XMLocalizator");
        }

        private static XmlSerializer _serializer = new XmlSerializer(typeof(Localization), new Type[] { typeof(Phrase) });
        private static XmlSerializerNamespaces _xmlNamespace = new XmlSerializerNamespaces();
    }
}
